# Build recipe for docbook-xml.
#
# Copyright (c) 2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=docbook-xml
version=4.5
release=1

# Define a category for the output of the package name
pkgcategory=docbook

tarname=${program}-${version}.zip

# Remote source(s)
fetch="
  https://docbook.org/xml/${version}/$tarname
  https://docbook.org/xml/4.4/${program}-4.4.zip
  https://docbook.org/xml/4.3/${program}-4.3.zip
  https://docbook.org/xml/4.2/${program}-4.2.zip
  https://docbook.org/xml/4.1.2/docbkx412.zip
"

description="
The DocBook XML DTD.

The DocBook XML DTD package contains document type definitions
for verification of XML data files against the DocBook rule set.

These are useful for structuring books and software documentation
to a standard allowing you to utilize transformations already
written for that standard.
"

homepage=https://www.docbook.org
license=Custom

# Source documentation
docs="ChangeLog README"
docsdir="${docdir}/${program}-${version}"

build()
{
    # Unzip the sources in an own directory
    for _version in $version 4.4 4.3 4.2 4.1.2
    do
        mkdir -p "${srcdir}/${_version}" \
                 "${destdir}/usr/share/xml/docbook/xml-dtd-${_version}"

        if test $_version = 4.1.2
        then
            cd "${srcdir}/${_version}"
            unpack "${tardir}/docbkx412.zip"
        else
            cd "${srcdir}/${_version}"
            unpack "${tardir}/docbook-xml-${_version}.zip"
        fi

        # Set sane permissions
        chmod -R u+w,go-w,a+rX-s .

        # Populate package
        cp -R -p -f docbook.cat *.dtd ent/ *.mod \
         "${destdir}/usr/share/xml/docbook/xml-dtd-${_version}"
    done
    unset -v _version

    # Switch back to the current 'version'
    cd "${TMPDIR}/${srcdir}/${version}"

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"

    # Create config directory
    mkdir -p "${destdir}/etc/xml"

    # Insert post-install script manually

    mkdir -p "${destdir}/var/lib/qi"
    cat << EOF > "${destdir}/var/lib/qi/${full_pkgname}.sh"

# XML docbook
if test ! -e etc/xml/docbook
then
    echo "Creating etc/xml/docbook (version: $version) ..."
    xmlcatalog --noout --create etc/xml/docbook
fi

echo ""
echo "Updating etc/xml/docbook ..."

xmlcatalog --noout --add "public" \\
 "-//OASIS//DTD DocBook XML V4.5//EN" \\
 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" \\
 etc/xml/docbook

xmlcatalog --noout --add "public" \\
 "-//OASIS//DTD DocBook XML CALS Table Model V4.5//EN" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5/calstblx.dtd" \\
 etc/xml/docbook

xmlcatalog --noout --add "public" \\
 "-//OASIS//DTD XML Exchange Table Model 19990315//EN" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5/soextblx.dtd" \\
 etc/xml/docbook

xmlcatalog --noout --add "public" \\
 "-//OASIS//ELEMENTS DocBook XML Information Pool V4.5//EN" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5/dbpoolx.mod" \\
 etc/xml/docbook

xmlcatalog --noout --add "public" \\
 "-//OASIS//ELEMENTS DocBook XML Document Hierarchy V4.5//EN" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5/dbhierx.mod" \\
 etc/xml/docbook

xmlcatalog --noout --add "public" \\
 "-//OASIS//ELEMENTS DocBook XML HTML Tables V4.5//EN" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5/htmltblx.mod" \\
 etc/xml/docbook

xmlcatalog --noout --add "public" \\
 "-//OASIS//ENTITIES DocBook XML Notations V4.5//EN" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5/dbnotnx.mod" \\
 etc/xml/docbook

xmlcatalog --noout --add "public" \\
 "-//OASIS//ENTITIES DocBook XML Character Entities V4.5//EN" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5/dbcentx.mod" \\
 etc/xml/docbook

xmlcatalog --noout --add "public" \\
 "-//OASIS//ENTITIES DocBook XML Additional General Entities V4.5//EN" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5/dbgenent.mod" \\
 etc/xml/docbook

xmlcatalog --noout --add "rewriteSystem" \\
 "http://www.oasis-open.org/docbook/xml/4.5" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5" \\
 etc/xml/docbook

xmlcatalog --noout --add "rewriteURI" \\
 "http://www.oasis-open.org/docbook/xml/4.5" \\
 "file:///usr/share/xml/docbook/xml-dtd-4.5" \\
 etc/xml/docbook

# XML catalog
if test ! -e etc/xml/catalog
then
    echo "Creating etc/xml/catalog (version: $version) ..."
    xmlcatalog --noout --create etc/xml/catalog
fi

echo ""
echo "Updating etc/xml/catalog ..."

xmlcatalog --noout --add "delegatePublic" \\
 "-//OASIS//ENTITIES DocBook XML" \\
 "file:///etc/xml/docbook" \\
 etc/xml/catalog

xmlcatalog --noout --add "delegatePublic" \\
 "-//OASIS//DTD DocBook XML" \\
 "file:///etc/xml/docbook" \\
 etc/xml/catalog

xmlcatalog --noout --add "delegateSystem" \\
 "http://www.oasis-open.org/docbook/" \\
 "file:///etc/xml/docbook" \\
 etc/xml/catalog

xmlcatalog --noout --add "delegateURI" \\
 "http://www.oasis-open.org/docbook/" \\
 "file:///etc/xml/docbook" \\
 etc/xml/catalog

echo ""
printf "%s" "Adding identifiers from previous version:"

for DTDVERSION in 4.4 4.3 4.2 4.1.2
do
    printf "%s" " v\${DTDVERSION}"

    # etc/xml/docbook

    xmlcatalog --noout --add "public" \\
     "-//OASIS//DTD DocBook XML V\${DTDVERSION}//EN" \\
     "http://www.oasis-open.org/docbook/xml/\${DTDVERSION}/docbookx.dtd" \\
     etc/xml/docbook

    xmlcatalog --noout --add "rewriteSystem" \\
     "http://www.oasis-open.org/docbook/xml/\${DTDVERSION}" \\
     "file:///usr/share/xml/docbook/xml-dtd-4.5" \\
     etc/xml/docbook

    xmlcatalog --noout --add "rewriteURI" \\
     "http://www.oasis-open.org/docbook/xml/\${DTDVERSION}" \\
     "file:///usr/share/xml/docbook/xml-dtd-4.5" \\
     etc/xml/docbook

    # etc/xml/catalog

    xmlcatalog --noout --add "delegateSystem" \\
     "http://www.oasis-open.org/docbook/xml/\${DTDVERSION}/" \\
     "file:///etc/xml/docbook" \\
     etc/xml/catalog

    xmlcatalog --noout --add "delegateURI" \\
     "http://www.oasis-open.org/docbook/xml/\${DTDVERSION}/" \\
     "file:///etc/xml/docbook" \\
     etc/xml/catalog
done

EOF
}

